package com.hongshan.wallet.core.home

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.databinding.ObservableField
import android.databinding.ViewDataBinding
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import com.hongshan.wallet.BR
import com.hongshan.wallet.MyApplication
import com.hongshan.wallet.R
import com.hongshan.wallet.base.BaseOnPropertyChangedCallback
import com.hongshan.wallet.base.MyFragment
import com.hongshan.wallet.bean.OrderDescBean
import com.hongshan.wallet.bean.UserDescBean
import com.hongshan.wallet.core.trading.TradingRecycleAdapter
import com.hongshan.wallet.showAlert

/**
 * Copyright Qiandai Ltd.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.core.home
 * @author   : Administrator
 * @data     : 2019/3/19
 * @email    : zhangxx@baihang-china.com
 */
class HomeFragment : MyFragment<ViewDataBinding, HomeViewModel>() {

    val homeVm by lazy { ViewModelProviders.of(this).get(HomeViewModel::class.java) }

    private val adapter = TradingRecycleAdapter(arrayListOf())

    override fun initVariableId() = BR.homeVm

    override fun initContentView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?) =
        R.layout.fragment_home

    override fun initViewModel() = homeVm

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        homeVm. adapter = adapter
        adapter.refreshListener = { homeVm.onRefresh() }
        homeVm.showDesc.observe(this, Observer {
            activity?.showAlert("冻结余额", "待付款订单冻结5分钟，确认交易后自动解冻；\n异常订单冻结20分钟，时间到期后自动解冻。", "知道了")
        })
        homeVm.getBanner()
        MyApplication.userInfo.addOnPropertyChangedCallback(BaseOnPropertyChangedCallback<ObservableField<UserDescBean>>{sender,_->
            val userDesc = sender.get() ?: return@BaseOnPropertyChangedCallback
            viewModel.money.set(userDesc.balance)
            viewModel.freezeMoney.set("${userDesc.frozen}元已冻结")
            if (userDesc.account_online == "yes") {
                viewModel.onLineStatus.set(R.drawable.ic_switch_o)
                viewModel.onLineStatusText.set("账户上线,即可自动接单")
//                MyActivityManager.activityStack.forEach { it.screenAlwaysOn() }
            } else {
                viewModel.onLineStatus.set(R.drawable.ic_switch_c)
                viewModel.onLineStatusText.set("账户离线,不可接单")
//                MyActivityManager.activityStack.forEach { it.cancleScreenOn() }
            }
        })
        homeVm.showAlert.observe(this, Observer{ array -> activity?.showAlert(array?.get(0), array?.get(1), array?.get(2))})
    }

    override fun onStart() {
        super.onStart()
        homeVm.onRefresh()
    }

    override fun onVisible() {
        super.onVisible()
        homeVm.onRefresh()
    }

    fun addAlerts(list: ArrayList<OrderDescBean>){
        list.forEach { l ->
            val filter = adapter.list.filter { it.id == l.id }
            if (filter.isEmpty()) {
                adapter.list.add(0,l)
                adapter.notifyItemRangeInserted(0,1)
            }
        }
    }
}